package handler

import (
	"github.com/gin-gonic/gin"
	"perm-sys/model"
	"perm-sys/pkg"
	"strconv"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	data := model.User{
		Username: username,
		Password: password,
	}
	err := model.DB.Debug().Find(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
	}
	if password != pkg.Md5(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
}

func Role(c *gin.Context) {

}

func Node(c *gin.Context) {

}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var user []model.User
	err := model.DB.Debug().Find(&user).Offset(offset).Limit(size).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "展示错误",
		})
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": user,
	})
}
